package com.imooc.chart.util;

import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpStatus;

public class ImageURLCheck {

    public static boolean isImageAccessible(String imageUrl) {
        try {
            HttpResponse response = HttpRequest.head(imageUrl)
                    .timeout(5000) // 设置超时，单位是毫秒
                    .execute();
            // 检查响应状态码是否为200
            return response.getStatus() == HttpStatus.HTTP_OK;
        } catch (Exception e) {
            System.out.println("无法访问指定的图片URL，出现异常：" + e.getMessage());
            return false;
        }
    }

    public static void main(String[] args) {
        String imageUrl = "https://img-blog.csdnimg.cn/2f1cf78ed68a4ff29819e3cc38ff6d7c.png";
        boolean accessible = isImageAccessible(imageUrl);
        System.out.println(imageUrl + " -> 图片" + (accessible ? "可以" : "不可以") + "访问");
    }
}
